package com.set;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

public class CollectionsTest {
		public static void main(String[] args) {
			//数组转集合
			Collection<String> c2 = new ArrayList();
			String[] as2 = {"D","A","B","C"};
			//c2 = Arrays.asList(as2);禁止使用
			Collections.addAll(c2, as2);//中间很多...是变长数组
			System.out.println(c2);
			Collections.addAll(c2, "a","b","c","d");
			System.out.println(c2);
			System.out.println(as2);
			System.out.println(Arrays.deepToString(as2));
				
			//线程安全
			c2 = Collections.synchronizedCollection(c2);//线程不安全
			c2 = new Vector<String>();//线程安全的
			c2 = new java.util.concurrent.CopyOnWriteArrayList<String>(); //线程安全
			c2 = new java.util.concurrent.CopyOnWriteArraySet<String>(); //线程安全
			Map<?, ?> map = new HashMap();//非线程安全的
			map =Collections.synchronizedMap(map);//线程安全的
			map = new Hashtable<>();//线程安全的
			map = new  java.util.concurrent.ConcurrentHashMap();
			
			//数组拷贝
			String[] as3 = Arrays.copyOf(as2, 2);
			System.out.println(Arrays.toString(as3));
			String[] as4 = Arrays.copyOfRange(as2, 1, 3);
			System.out.println(Arrays.toString(as4));
			
			//排序搜索
			Arrays.sort(as2);
			System.out.println(Arrays.toString(as2));
			System.out.println(Arrays.binarySearch(as2, "B"));
			
			//lambda
			Arrays.sort(as2,(a,b)->b.compareTo(a));
			System.out.println(Arrays.toString(as2));
			System.out.println(Arrays.binarySearch(as2, "B",(a,b)->b.compareTo(a)));
		}

}
